//
//  CatogryAllSpecialDetailCell.m
//  project
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryAllSpecialDetailCell.h"
#import <UIImageView+WebCache.h>
#import "FMDatabase.h"
#import <UIButton+WebCache.h>

@interface  CatogryAllSpecialDetailCell ()

//写一个数据库的属性
@property(nonatomic,strong)FMDatabase *dataBase;
@property (nonatomic, strong)NSMutableArray *array;

@end

@implementation CatogryAllSpecialDetailCell

- (void)awakeFromNib {
    // Initialization code
}

-(NSMutableArray *)array
{
    if (_array == nil)
    {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _specialDetailImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kWidth - 10, 200)];
        _specialDetailImage.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_specialDetailImage];
        
        _loveImage = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth - 100, 10, 80, 30)];
        _loveImage.layer.masksToBounds = YES;
        _loveImage.layer.cornerRadius = 10;
        _loveImage.backgroundColor = [UIColor grayColor];
        _loveImage.alpha = 0.7;
        [self.contentView addSubview:_loveImage];
        
        _loveButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _loveButton1.frame = CGRectMake(kWidth - 100, 10, 20, 30);
        [_loveButton1 setImage:[UIImage imageNamed:@"iconfont-icon503"] forState:(UIControlStateNormal)];
        [_loveButton1 addTarget:self action:@selector(collect:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:_loveButton1];
        
        _loveButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _loveButton2.frame = CGRectMake(kWidth - 80, 10, 60, 30);
        [_loveButton2 setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.contentView addSubview:_loveButton2];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 180, kWidth - 20, 20)];
        _titleLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleLabel];
    
        
    }
    return self;
}

-(void)collect:(UIButton *)button
{
    [_array removeAllObjects];
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(9, 1, 1)firstObject] stringByAppendingPathComponent:@"project.db"];
    _dataBase = [FMDatabase databaseWithPath:filepath];
    if ([_dataBase open])
    {
        [_dataBase executeUpdate:@"create table AllLove (loveimage text,lovetitle text, loveid text)"];
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
    while ([resultSet next])
    {
        NSString *modelId= [resultSet stringForColumn:@"loveid"];
        [self.array addObject:modelId];
        
    }
    for (NSString *str in self.array)
    {
        if ([[NSString stringWithFormat:@"%@",self.model.Myid] isEqualToString:str])
        {
            [_dataBase executeUpdate:@"delete from AllLove where loveid=?", str];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [_loveButton1 setImage:[UIImage imageNamed:@"iconfont-icon503"] forState:(UIControlStateNormal)];
            return;
        }
    }
    
    [_dataBase executeUpdate:@"insert into AllLove values (? ,?, ?)",self.model.cover_image_url,self.model.title,[NSString stringWithFormat:@"%@",self.model.Myid]];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [_loveButton1 setImage:[UIImage imageNamed:@"iconfont-icon503-2"] forState:(UIControlStateNormal)];
    
    
}

-(void)setModel:(OtherTableViewModel *)model
{
    _model = model;
    [_specialDetailImage sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    [_loveButton2 setTitle:[NSString stringWithFormat:@"%@",model.likes_count] forState:(UIControlStateNormal)];
    _titleLabel.text = model.title;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
